package com.sxm.yiwei.pinche;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.R;
import com.sxm.yiwei.base.BaseActivity;
import com.sxm.yiwei.constant.Common;
import com.sxm.yiwei.constant.Constant;
import com.sxm.yiwei.utils.HandleDataUtil;
import com.sxm.yiwei.utils.SPUtil;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class OrderPayActivity extends BaseActivity {

    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_break_money;
    private TextView tv_order_id;
    private LinearLayout ll_ali_pay;
    private LinearLayout ll_yivi_pay;
    private TextView tv_pay_money;
    private Button btn_pay;
    private int pay_type=1;//1 支付宝 2 余额
    private String orderCode;
    private ProgressDialog progressDialog;
    private int identity;
    private int type;
    private String total_money;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_order_pay);
    }

    @Override
    public void initView() {
        rl_back = (RelativeLayout)findViewById(R.id.rl_back);
        tv_title = (TextView)findViewById(R.id.tv_title);
        tv_break_money = (TextView)findViewById(R.id.tv_break_money);
        tv_order_id = (TextView)findViewById(R.id.tv_order_id);
        ll_ali_pay = (LinearLayout)findViewById(R.id.ll_ali_pay);
        ll_yivi_pay = (LinearLayout)findViewById(R.id.ll_yivi_pay);
        tv_pay_money = (TextView)findViewById(R.id.tv_pay_money);
        btn_pay = (Button)findViewById(R.id.btn_pay);

        tv_title.setText("订单支付");
    }

    @Override
    public void initListener() {
        ll_ali_pay.setOnClickListener(this);
        ll_yivi_pay.setOnClickListener(this);
        rl_back.setOnClickListener(this);
        btn_pay.setOnClickListener(this);
    }

    @Override
    public void initData() {
        orderCode = getIntent().getStringExtra("orderCode");
        identity = getIntent().getIntExtra("identity",-1);
        type = getIntent().getIntExtra("type",-1);
        total_money = getIntent().getStringExtra("total_money");

        tv_pay_money.setText(total_money);
        tv_order_id.setText(orderCode);
        tv_break_money.setText("");//乘客有爽约金么？
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.ll_ali_pay:
                pay_type=1;
                break;
            case R.id.ll_yivi_pay:
                pay_type=2;
                break;
            case R.id.btn_pay:
                if (pay_type==1){
                    if (identity==1){
                        if (type==3){
                            //乘客发布长途急件支付宝支付
                        }else {
                            passengerAliPay(total_money);
                        }
                    }else if (identity==2){
                        if (type==3){
                            //车主发布长途急件支付宝支付
                        }else {
                            //车主发布长途支付宝支付
                        }

                    }
                }else {
                    if (identity==1){
                        if (type==3){
                            //乘客发布长途急件余额支付
                        }else {
                            passengerYiviPay(total_money,orderCode,"123456");
                        }
                    }else if (identity==2){
                        if (type==3){
                            //车主发布长途急件余额支付
                        }else {
                            //车主发布长途余额支付
                        }

                    }
                }
                break;
        }
    }
    private void passengerAliPay(String pay_money){
        progressDialog = ProgressDialog.show(this,"加载","正在加载中...");
        JSONObject object = new JSONObject();
        object.put("serviceId", SPUtil.getString(this,"service_id"));
        object.put("payType", Constant.PASSENGER_PUBLISH_FLAG+"");
        object.put("pinTotalAccount",pay_money);
        final RequestParams params=  new RequestParams(Common.HOST+Common.PASSENGER_PUBLISH_ALIPAY);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",getHeadInfo().getToken());
        params.addHeader("appId",getHeadInfo().getAppId());
        params.addHeader("userId",getHeadInfo().getUserId());
        Log.d("getHead",getHeadInfo().getToken()+"getHeadInfo  "+getHeadInfo().getAppId()+"getUserId  "+getHeadInfo().getUserId());
        params.setBodyContent(object.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                progressDialog.dismiss();
                Log.d("tag", "onSuccess: 乘客发布支付宝--"+result);
                com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
                if (obj.getIntValue("status")==1){
                    Intent intent = new Intent(OrderPayActivity.this, CompleteActivity.class);
                    intent.putExtra("type",Constant.RELEASE_OK);
                    intent.putExtra("text_1","");
                    intent.putExtra("text_2","");
                    startActivity(intent);
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                progressDialog.dismiss();
                Log.d("tag", "onError: 乘客发布支付宝--"+ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
    private void passengerYiviPay(String pay_money,String order_code,String pwd){
        progressDialog=ProgressDialog.show(this,"加载","正在加载...");
        JSONObject object = new JSONObject();
        object.put("serviceId", SPUtil.getString(this,"service_id"));
        object.put("payType", Constant.PASSENGER_PUBLISH_FLAG+"");
        object.put("total_amount",pay_money);
        object.put("orderId",order_code);
        object.put("payPassword",pwd);//密码用AES加密
        final RequestParams params=  new RequestParams(Common.HOST+Common.PASSENGER_PUBLISH_RESTPAY);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",getHeadInfo().getToken());
        params.addHeader("appId",getHeadInfo().getAppId());
        params.addHeader("userId",getHeadInfo().getUserId());
        Log.d("getHead",getHeadInfo().getToken()+"getHeadInfo  "+getHeadInfo().getAppId()+"getUserId  "+getHeadInfo().getUserId());
        params.setBodyContent(object.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                progressDialog.dismiss();
                Log.d("tag", "onSuccess: 乘客发布余额支付--"+result);
                com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
                if (obj.getIntValue("status")==1){
                    Intent intent = new Intent(OrderPayActivity.this, CompleteActivity.class);
                    intent.putExtra("type",Constant.RELEASE_OK);
                    intent.putExtra("text_1","");
                    intent.putExtra("text_2","");
                    startActivity(intent);
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                progressDialog.dismiss();
                Log.d("tag", "onError: 乘客发布余额支付--"+ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
